﻿// -------------------------------------------------------------------------
//    @FileName         :    NFlogEventHandler.cpp
//    @Author           :    gaoyi
//    @Date             :    24-7-30
//    @Email            :    445267987@qq.com
//    @Module           :    NFlogEventHandler
//
// -------------------------------------------------------------------------

#include <NFComm/NFPluginModule/NFCheck.h>

#include "NFlogEventHandler.h"

#include <NFRegisterCache.h>
#include <NFPlayerMgr.h>
#include <NFPlayerUtil.h>
#include <NFTransGetRoleList.h>

#include "NFlogUtil.h"
#include "proto_log.nanopb.h"
#include "NFSession.h"
#include "NFPlayer.h"
#include "NFlogUtil.h"

static const char *netTypeToString(proto_ff::Proto_NetworkType eType)
{
    switch (eType)
    {
        // NetWork Type
        case proto_ff::E_NETWORK_2G: return "2G";
        case proto_ff::E_NETWORK_3G: return "3G";
        case proto_ff::E_NETWORK_4G: return "4G";
        case proto_ff::E_NETWORK_WIFI: return "WIFI";
        default: break;
    }
    return "WIFI";
}

int MakeTlogDeviceInfo(NFlogClientInfo &stLog, NFSession *pSession)
{
    auto pClientInfo = pSession->GetClientInfo();
    stLog.ClientVersion = pClientInfo->client_version;
    stLog.LoginChannel = pSession->GetClientInfo()->login_channel_str;

    stLog.SystemSoftware = pClientInfo->os_version;
    stLog.SystemHardware = pClientInfo->hardware;
    stLog.TelecomOper = pClientInfo->tel_oper;
    stLog.Network = netTypeToString((proto_ff::Proto_NetworkType) pClientInfo->network_type);
    stLog.ScreenWidth = pClientInfo->screen_width;
    stLog.ScreenHight = pClientInfo->screen_hight;
    stLog.Density = pClientInfo->screen_density;
    stLog.CpuHardware = pClientInfo->cpu_info;
    stLog.Memory = pClientInfo->memory_size;
    stLog.GLRender = pClientInfo->gl_render;
    stLog.GLVersion = pClientInfo->gl_version;
    stLog.DeviceId = pClientInfo->device_id;
    stLog.HostIP = pSession->GetLoginIP();
    return 0;
}

int NFlogEventHandler::OnSessionCreateSuccess(NFSession *pSession)
{
    CHECK_NULL(0, pSession);
    return OnPlayerLoginProcess(pSession, EN_SEC_PLAYER_LOGIN_PROCESS_SESSION_SUCCESS, "");
}

int NFlogEventHandler::OnPlayerGetAccountInfoReq(NFSession *pSession)
{
    CHECK_NULL(0, pSession);
    return OnPlayerLoginProcess(pSession, EN_SEC_PLAYER_LOGIN_PROCESS_GET_ACCOUNT_REQ, "");
}

int NFlogEventHandler::OnPlayerLoginProcess(NFSession *pSession, SEC_PLAYER_LOGIN_PROCESS_TYPE type, const std::string &extraData)
{
    CHECK_NULL(0, pSession);
    NFlogPlayerLoginProcess stLog;
    NFlogUtil::InitTlogTencentHead(&stLog.stHead, NULL, pSession);
    stLog.iConnID = pSession->GetConnIndex();
    stLog.iObjID = pSession->GetGlobalId();
    stLog.iConnBusID = pSession->GetConnBusId();
    stLog.iType = (int) type;

    stLog.stExtraData = extraData;

    LOG_BEHAVIOUR(stLog.ToPb());
    return 0;
}

int NFlogEventHandler::OnSessionLogCheckPoint(NFSession *pSession, int iCheckPoint)
{
    CHECK_NULL(0, pSession);

    NFlogClientCheckPoint stLog;
    NFlogUtil::InitTlogTencentHead(&stLog.stHead, NULL, pSession);
    stLog.iCheckPoint = iCheckPoint;

    LOG_BEHAVIOUR(stLog.ToPb());
    return 0;
}

int NFlogEventHandler::OnPlayerLogin(NFPlayer *pPlayer)
{
    CHECK_NULL(0, pPlayer);
    NFSession *pSession = pPlayer->GetClientSession();
    CHECK_NULL(0, pSession);

    NFlogPlayerLogin stLog;
    NFlogUtil::InitTlogTencentHead(&stLog.stHead, pPlayer, pSession);
    MakeTlogDeviceInfo(stLog.stClientInfo, pSession);

    LOG_BEHAVIOUR(stLog.ToPb());
    return 0;
}

int NFlogEventHandler::OnPlayerLogout(NFPlayer *pPlayer)
{
    CHECK_NULL(0, pPlayer);
    NFSession *pSession = pPlayer->GetClientSession();
    CHECK_NULL(0, pSession);

    NFlogPlayerLogout stLog;
    NFlogUtil::InitTlogTencentHead(&stLog.stHead, pPlayer, pSession);
    MakeTlogDeviceInfo(stLog.stClientInfo, pSession);

    //stLog.iMoney    = pPlayer->GetItemExt()->GetNum( proto_ff::EN_ID_MONEY );
    //stLog.iGold     = pPlayer->GetItemExt()->GetNum( proto_ff::EN_ID_GOLD );
    //stLog.iJieni    = pPlayer->GetItemExt()->GetNum( proto_ff::EN_ID_JIENI );
    //stLog.iDiamond  = pPlayer->GetPayExt()->GetDiamond();

    //stLog.iGuildID  = pPlayer->GetSnsExt()->GetGuildID();
    //CTlogUtil::TlogStrCpy(hunterLog.vGuildName, pPlayer->GetSnsExt()->GetGuildName(), sizeof(hunterLog.vGuildName));


    stLog.iOnlineTime = (int) (NF_ADJUST_TIMENOW() - pPlayer->GetLastLoginTime());
    stLog.iTotalOnlineTime = pPlayer->GetRoleBase()->today_online_time;

    LOG_BEHAVIOUR(stLog.ToPb());
    return 0;
}

int NFlogEventHandler::OnPlayerDisconnect(NFPlayer *pPlayer, int iType, int iReason)
{
    NFSession *pSession = pPlayer->GetClientSession();
    CHECK_NULL(0, pSession);

    NFlogPlayerDisconnect stLog;
    NFlogUtil::InitTlogTencentHead(&stLog.stHead, pPlayer, pSession);
    MakeTlogDeviceInfo(stLog.stClientInfo, pSession);

    stLog.iOnlineTime = NF_ADJUST_TIMENOW() - pPlayer->GetLastLoginTime();
    stLog.dtLoginTime = pPlayer->GetLastLoginTime();
    stLog.iLogoutType = iType;
    stLog.iLogoutReason = iReason;

    LOG_BEHAVIOUR(stLog.ToPb());
    return 0;
}

int NFlogEventHandler::OnPlayerDelete(NFSession *pSession, RoleOne *pstRoleOne)
{
    CHECK_NULL(0, pSession);
    CHECK_NULL(0, pstRoleOne);

    NFlogPlayerDeleteRole stTcLog;
    NFlogUtil::InitTlogTencentHead(&stTcLog.stHead, NULL, pSession);

    stTcLog.stHead.iRoleJob = pstRoleOne->actor_id;
    stTcLog.stHead.vRoleID = pstRoleOne->role_id;
    stTcLog.stHead.iRoleLevel = pstRoleOne->actor_level;
    stTcLog.stHead.iRoleCE = pstRoleOne->role_ce;
    LOG_BEHAVIOUR(stTcLog.ToPb());
    return 0;
}

int NFlogEventHandler::OnPlayerRestored(NFPlayer *pPlayer)
{
    CHECK_NULL(0, pPlayer);

    NFSession *pSession = pPlayer->GetClientSession();
    CHECK_NULL(0, pSession);

    NFlogPlayerRestored stLog;
    NFlogUtil::InitTlogTencentHead(&stLog.stHead, pPlayer, pSession);
    MakeTlogDeviceInfo(stLog.stClientInfo, pSession);

    stLog.dtCreateTime = pPlayer->GetCacheCreateTime();
    stLog.dtLastLogoutTime = pPlayer->GetLastLogoutTime();
    LOG_BEHAVIOUR(stLog.ToPb());
    return 0;
}

int NFlogEventHandler::OnPlayerCreate(NFPlayer *pPlayer)
{
    CHECK_NULL(0, pPlayer);
    NFSession *pSession = pPlayer->GetClientSession();
    CHECK_NULL(0, pSession);

    NFlogPlayerCreateRole stTcLog;
    NFlogUtil::InitTlogTencentHead(&stTcLog.stHead, pPlayer, pSession);
    LOG_BEHAVIOUR(stTcLog.ToPb());
    return 0;
}

int NFlogEventHandler::OnMoneyFlow(NFPlayer* pPlayer, NFLogPlayerMoneyFlow& nflog)
{
    CHECK_NULL(0, pPlayer);

    NFSession *pSession = pPlayer->GetClientSession();
    CHECK_NULL(0, pSession);

    NFlogUtil::InitTlogTencentHead(&nflog.stHead, pPlayer, pSession);
    MakeTlogDeviceInfo(nflog.stClientInfo, pSession);
    nflog.iAddOrReduce = nflog.llMoney > 0 ? 0 : 1;

    LOG_BEHAVIOUR(nflog.ToPb());
    return 0;
}
